package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ymatou.iapi.livebuyer.parameter.QueryProductsInLiveRoomBean;
import com.ymatou.iapi.livebuyer.service.QueryProductsInLiveRoomCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*查询直播间的商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryProductsInLiveRoom{
	private static QueryProductsInLiveRoomBean queryproductsinliveroomBean;
	private static QueryProductsInLiveRoomCall queryproductsinliveroomCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询直播间的商品列表");
	}

	@Before
	public void caseUp() {
		queryproductsinliveroomBean=new QueryProductsInLiveRoomBean();
		queryproductsinliveroomCall=new QueryProductsInLiveRoomCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryproductsinliveroomCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Map<Integer, List<String>> m,int size) {
		JsonObject data = queryproductsinliveroomCall.getJsonObject().get("data").getAsJsonObject();
		Logger.debug("map:"+m);
		m.keySet().forEach( key->{
			List<String> list = m.get(key);
			Logger.debug(key+":key-list:"+list);
			JsonArray ja = data.get("productList").getAsJsonArray();
			Logger.verifyEquals(size,ja.size(),"验证size");
//			Logger.debug(ja.toString());
			ja.forEach(json->{
				Logger.verifyEquals(true, list.contains(json.getAsJsonObject().get("productId").getAsString()), "判断结果包含商品");
			});
			
		});
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_001() {
		Logger.start(true,"查询直播间的商品列表成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			List<String> productIds=new ArrayList<>();
			productIds.add(pid);
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			m.put(createlive.getId(), productIds);
			
			queryproductsinliveroomBean.setLiveId(createlive.getId());
			queryproductsinliveroomBean.setProductIds(productIds);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("200", queryproductsinliveroomCall.getString("code"), "验证code");
			verifyDB(m,1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_002() {
		Logger.start(true,"不输入liveId失败");
		try {
			List<String> productIds=new ArrayList<>();
			productIds.add(TestDataManager.getRandomUUID().toString());
			queryproductsinliveroomBean.setProductIds(productIds);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("100", queryproductsinliveroomCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_003() {
		Logger.start(true,"不输入productIds失败");
		try {
			queryproductsinliveroomBean.setLiveId(1);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("100", queryproductsinliveroomCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_004() {
		Logger.start(true,"查询多个直播间的商品列表成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			String pid2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid2);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid2);
			List<String> productIds=new ArrayList<>();
			productIds.add(pid);
			productIds.add(pid2);
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			m.put(createlive.getId(), productIds);
			
			queryproductsinliveroomBean.setLiveId(createlive.getId());
			queryproductsinliveroomBean.setProductIds(productIds);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("200", queryproductsinliveroomCall.getString("code"), "验证code");
			verifyDB(m,2);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_005() {
		Logger.start(true,"查询多个直播间的商品,包含不在live中pid,不在live中不展示");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			String pid2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid2);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid2);
			List<String> productIds1=new ArrayList<>();
			productIds1.add(pid);
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			
			m.put(createlive.getId(), productIds1);
			List<String> productIds=new ArrayList<>();
			productIds.add(pid);
			productIds.add(pid2);
			queryproductsinliveroomBean.setLiveId(createlive.getId());
			queryproductsinliveroomBean.setProductIds(productIds);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("200", queryproductsinliveroomCall.getString("code"), "验证code");
			verifyDB(m,1);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_006() {
		Logger.start(true,"查询多个直播间的商品,包含其他商家live商品pid,其他商家不展示");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			AddSellerProductRequest addpro2 = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			String pid2 = ProductManagerCallServiceV2.addNewProduct(addpro2).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid2);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoom createlive2 = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(3383));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive2.getId(), createlive2.getUserId(), pid2);
			List<String> productIds1=new ArrayList<>();
			productIds1.add(pid);
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			
			m.put(createlive.getId(), productIds1);
			List<String> productIds=new ArrayList<>();
			productIds.add(pid);
			productIds.add(pid2);
			queryproductsinliveroomBean.setLiveId(createlive.getId());
			queryproductsinliveroomBean.setProductIds(productIds);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("200", queryproductsinliveroomCall.getString("code"), "验证code");
			verifyDB(m,1);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_007() {
		Logger.start(true,"求讲解四次，查询为最热门商品");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			String pid2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid2);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid2);
			List<String> productIds=new ArrayList<>();
			productIds.add(pid);
			productIds.add(pid2);
			
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			m.put(createlive.getId(), productIds);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			Thread.sleep(3000);
			LiveRoomCallService.AskProductVideo(createlive.getId(), productIds.get(0),1);
			LiveRoomCallService.AskProductVideo(createlive.getId(), productIds.get(0),2);
			LiveRoomCallService.AskProductVideo(createlive.getId(), productIds.get(0),3);
			LiveRoomCallService.AskProductVideo(createlive.getId(), productIds.get(1),4);
			Thread.sleep(3000);
			queryproductsinliveroomBean.setLiveId(createlive.getId());
			queryproductsinliveroomBean.setProductIds(productIds);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("200", queryproductsinliveroomCall.getString("code"), "验证code");
			
			JsonArray products = queryproductsinliveroomCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList");
			Logger.verifyEquals(1, products.get(0).getAsJsonObject().get("status").getAsInt(), "验证销售状态");
			Logger.verifyEquals(true, products.get(0).getAsJsonObject().get("hot").getAsBoolean(), "验证求讲解");
			Logger.verifyEquals(1, products.get(1).getAsJsonObject().get("status").getAsInt(), "验证销售状态");
			Logger.verifyEquals(true, products.get(1).getAsJsonObject().get("hot").getAsBoolean(), "验证求讲解");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryProductsInLiveRoom_008() {
		Logger.start(true,"置顶，查询置顶商品");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			String pid2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid2);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid2);
			List<String> productIds=new ArrayList<>();
			productIds.add(pid);
			productIds.add(pid2);
			
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			m.put(createlive.getId(), productIds);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			Thread.sleep(3000);
			Integer counter = CounterCallService.getSingleValueByLiveId(createlive.getId());
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);
			Integer counter1 = CounterCallService.getSingleValueByLiveId(createlive.getId());
			Logger.verifyEquals(counter+1, counter1, "验证计数器");
			queryproductsinliveroomBean.setLiveId(createlive.getId());
			queryproductsinliveroomBean.setProductIds(productIds);
			queryproductsinliveroomCall.setData(queryproductsinliveroomBean);
			queryproductsinliveroomCall.callService();			
			Logger.verifyEquals("200", queryproductsinliveroomCall.getString("code"), "验证code");
			JsonArray products = queryproductsinliveroomCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList");
			Logger.verifyEquals(true, products.get(1).getAsJsonObject().get("top").getAsBoolean(), "验证置顶状态");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
}